package cn.itsy.web.controller;

import cn.itsy.dao.Empactdao;
import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("empact")
public class EmpactController {
    @Resource
    private Empactdao ed;
    @Resource
    private Pagedao pd;

    @RequestMapping("list")
    public String list(Model m, String did, Empact ec, String currentPage) {
        Page page = new Page();
        String sql = "select ec.*,e.empname,p.cname,p.cpath from empact ec,emp e,pactext p where ec.emp_id=e.id and ec.pactext_id=p.id";
        if (did != null && !did.equals("")) {
            sql += " and e.depart_id='" + did + "'";
        }
        if (ec.getEno() != null && !ec.getEno().equals("")) {
            sql += " and ec.eno='" + ec.getEno() + "'";
        }
        if (ec.getEcno() != null && !ec.getEcno().equals("")) {
            sql += " and ec.ecno='" + ec.getEcno() + "'";
        }
        if (ec.getCreatetime() != null && !ec.getCreatetime().equals("")) {
            sql += " and ec.createtime='" + ec.getCreatetime() + "'";
        }
        if (ec.getBegintime() != null && !ec.getBegintime().equals("")) {
            sql += " and ec.begintime='" + ec.getBegintime() + "'";
        }
        if (ec.getEndtime() != null && !ec.getEndtime().equals("")) {
            sql += " and ec.endtime='" + ec.getEndtime() + "'";
        }
        if (ec.getStatus() != null && !ec.getStatus().equals("")) {
            sql += " and ec.status='" + ec.getStatus() + "'";
        }
        Page p = pd.empactfy(sql, currentPage, page);
        m.addAttribute("p", p);
        return "/empact";
    }

    @RequestMapping("toadd")
    public String toadd(Model m) {
        String sql = "select * from emp";
        String sql1 = "select * from pactext";
        List<Emp> el = ed.emplist(sql);
        List<Pactext> pl = ed.paclist(sql1);
        m.addAttribute("el", el);
        m.addAttribute("pl", pl);
        return "/empactadd";
    }

    @RequestMapping("add")
    public String add(Empact ea) {
        String s = "select * from empact";
        List<Empact> eaclist = ed.eaclist(s);
        String sql = "";
        for (Empact e : eaclist) {
            if (e.getEmpId() == ea.getEmpId() && e.getEmpId().equals(ea.getEmpId()) && !e.getStatus().equals("Y") && !e.getStatus().equals("W")) {
                sql = "insert into empact values(null,'" + ea.getEcno() + "','" + ea.getEmpId() + "','" + ea.getEno() + "','" + ea.getPactextId() + "','" + ea.getCreatetime() + "','" + ea.getBegintime() + "','" + ea.getEndtime() + "','" + ea.getDissolutiondesc() + "','" + ea.getExtenddesc() + "','" + ea.getTerminationdesc() + "','Y')";
            } else {
                sql = "insert into empact values(null,'" + ea.getEcno() + "','" + ea.getEmpId() + "','" + ea.getEno() + "','" + ea.getPactextId() + "','" + ea.getCreatetime() + "','" + ea.getBegintime() + "','" + ea.getEndtime() + "','" + ea.getDissolutiondesc() + "','" + ea.getExtenddesc() + "','" + ea.getTerminationdesc() + "','W')";
            }
        }
        ed.add(sql);
        return "redirect:list";
    }

    @RequestMapping("toupdate")
    public String toupdate(String id, String yy, Model m) {
        m.addAttribute("id", id);
        m.addAttribute("yy", yy);
        return "/empactupdate";
    }

    @RequestMapping("update")
    public String update(String id, String dissolutiondesc, String extenddesc, String terminationdesc) {
        String sql = "";
        if (dissolutiondesc != null && !dissolutiondesc.equals("")) {
            Date d = new Date();
            long time = d.getTime() / 1000 / 60 / 60 / 24;
            long time2 = (time + 2) * 1000 * 60 * 60 * 24;
            long time3 = (time + 1095) * 1000 * 60 * 60 * 24;
            Date date = new Date(time2);
            Date date1 = new Date(time3);
            String s3 = d.toLocaleString();
            String s1 = date.toLocaleString();
            String s2 = date1.toLocaleString();
            String s = "select * from empact where id='" + id + "'";
            List<Empact> eaclist = ed.eaclist(s);
            for (Empact e : eaclist) {
                String addsql = "insert into empact values(null,'" + e.getEcno() + "','" + e.getEmpId() + "','" + e.getEno() + "','" + e.getPactextId() + "','" + s3 + "','" + s1 + "','" + s2 + "','" + dissolutiondesc + "','" + e.getExtenddesc() + "','" + e.getTerminationdesc() + "','Y')";
                ed.add(addsql);
            }
            sql = "update empact set extenddesc='合同到期后续签合同',status='N' where id='" + id + "'";
        }
        if (extenddesc != null && !extenddesc.equals("")) {
            sql = "update empact set dissolutiondesc='',extenddesc='" + extenddesc + "',terminationdesc='',status='N' where id='" + id + "'";
        }
        if (terminationdesc != null && !terminationdesc.equals("")) {
            sql = "update empact set dissolutiondesc='',extenddesc='',terminationdesc='" + terminationdesc + "',status='S' where id='" + id + "'";
        }
        ed.update(sql);
        return "redirect:list";
    }

    @RequestMapping("delete")
    public String delete(String id, String[] ids) {
        String sql = "";
        if (ids != null) {
            for (int i = 0; i < ids.length; i++) {
                sql = "delete from empact where id='" + ids[i] + "'";
                ed.delete(sql);
            }
        } else {
            sql = "delete from empact where id='" + id + "'";
            ed.delete(sql);
        }
        return "redirect:list";
    }

    @RequestMapping("emplist")
    public String emplist(String empid, Model m, String currentPage) {
        Page page = new Page();
        String sql = "select ec.*,e.empname,p.cname,p.cpath from empact ec,emp e,pactext p where ec.emp_id=e.id and ec.pactext_id=p.id and ec.emp_id='" + empid + "' order by ec.createtime desc";
        Page pagel = pd.empactjlfy(sql, currentPage, page);
        m.addAttribute("pagel", pagel);
        m.addAttribute("empid", empid);
        return "/empactjl";
    }
}
